﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NerdDinner.Models;

namespace NerdDinner.Controllers
{
    public class DinnersController : Controller
    {
        DinnerRepository dinnerRepository = new DinnerRepository();

        //
        // GET: /Dinners/
        public ActionResult Index()
        {
            var dinners = dinnerRepository.FindUpcomingDinners().ToList();
            return View(dinners);
        }


        //
        // GET: /Dinners/Details/2
        public ActionResult Details(int id) {
            Dinner dinner = dinnerRepository.getDinner(id);
            if (dinner == null)
                return View("NotFound");
            else
                return View(dinner);
        }


        public ActionResult Edit(int id) {
            Dinner dinner = dinnerRepository.getDinner(id);
            return View(dinner);
        }


        //
        // POST: /Dinners/Edit/2
        [HttpPost]
        public ActionResult Edit(int id, FormCollection formValues) {
            // Retrieve existing dinner
            Dinner dinner = dinnerRepository.getDinner(id);
            
            // Update dinner with form posted values
            if (TryUpdateModel(dinner))
            {
                // Persist changes back to database
                dinnerRepository.Save();

                // Perform HTTP redirect to details page for the saved Dinner
                return RedirectToAction("Details", new { id = dinner.DinnerID });
            }
            return View(dinner);
        }
    }
}
